export default {
  inserted: function (el, binding, vnode) {
    const inputEl = el.querySelector('input')
    
    if (binding.value === 'int') {
      // 整数
      el.addEventListener("input", e => {
        // const val = inputEl?.value?.replace(/^(0+)|[^\d]+/g,'')
        const val = inputEl?.value?.replace(/[^0-9]/g, '')
        vnode.data.model.callback(val)
      })
    } else {
      el.addEventListener("input", e => {
        // 保留两位小数
        const val = inputEl?.value?.replace(/。/g, ".").replace(/[^\d.]/g, "").replace(/^\./g, "").replace(/\.{2,}/g, "").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3")

        vnode.data.model.callback(val)
      })
    }
  }
}